<?php
/**
 * RadioStoryController.class.php
 * 电台故事控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-04-27
 */

namespace Admin\Controller;

class RadioStoryController extends BaseController {

    /**
     * 电台列表获取
     * @param   $page
     * @return  json    $list
     */
    function getList(){
        $condition  = $this->request('condition', []);
        $page   = $this->request('p', 1);
        $pageHtml   = true;

        $this->response([
            'list'  => D('RadioStory')->getList($condition, $page, $pageHtml),
            'page_html' => $pageHtml
        ]);
    }

    /**
     * 添加电台
     * @param   $title
     * @param   $banner
     * @param   $audio
     * @return  boolean
     */
    function addItem(){
        $client     = $this->unEmptyParam(['title', 'audio']);

        $title  = $client['title'];
        $audio  = $client['audio'];
        $onlineTime     = $this->request('online_date') ? strtotime($this->request('online_date')) : time();
        $status     = $this->request('online_date') ? 2 : 1;

        $audio  = strchr($audio, '/server');
        $audioTime  = get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');

        $radioId    = D('RadioStory')->add([
            'title' => $title,
            'audio' => $audio,
            'audio_time'    => $audioTime,
            'create_time'   => time(),
            'online_time'   => $onlineTime,
            'status'    => $status
        ]);

        $this->response();
    }

    /**
     * 编辑电台
     * @param   $radio_id
     * @param   $radio_info
     * @return  boolean
     */
    function updateItem(){
        $client     = $this->unEmptyParam(['radio_id', 'radio_info']);

        $radioId    = $client['radio_id'];
        $radioInfo  = $client['radio_info'];
        if($radioInfo['audio']){
            $radioInfo['audio'] = strchr($radioInfo['audio'], '/server');
            $radioInfo['audio_time']    = get_audio_info(strchr($radioInfo['audio'], 'Uploads'), 'playtime_seconds');
        }
        
        $result     = D('RadioStory')->where('id = %d', $radioId)->save($radioInfo);
        $this->response();

        // $result  = D('RadioStory')->updateItem($radioId, $radioInfo);
        // if($result === false){
        //  $this->outOInfo(D('RadioStory')->errMsg, 0);
        // }

        // $this->outOInfo('Oh yes!');
    }
    
}